package com.kunlun.basedata.limit.controller;

import com.kunlun.basedata.limit.model.po.CorrelatePo;
import com.kunlun.basedata.limit.model.vo.CorrelateVo;
import com.kunlun.basedata.limit.model.vo.UserRelateVo;
import com.kunlun.basedata.limit.service.ICorrelateService;
import com.kunlun.common.model.Page;
import com.kunlun.common.utils.ResponseUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/correlate")
public class CorrelateController {

    private Logger log = LogManager.getLogger();

    @Autowired
    private ICorrelateService correlateService;

    @RequestMapping(value = "/getUnitNodeTree", method = RequestMethod.GET)
    public Object getUnitNodeTree(String type) {
        try {
            List<CorrelateVo> list = correlateService.getUnitNodeTree(type);
            return ResponseUtil.successResponse(list);
        } catch (Exception e) {
            log.error("AuthorizeController getAllotDepartments Error: ", e);
            return ResponseUtil.failedResponse("查询部门人员分派失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/getCorrelatePage", method = RequestMethod.GET)
    public Object getCorrelatePage(CorrelatePo correlatePo, int currentPage, int pageSize) {
        try {
            Page<CorrelateVo> page = correlateService.getCorrelatePage(correlatePo, currentPage, pageSize);
            return ResponseUtil.successResponse(page);
        } catch (Exception e) {
            log.error("AuthorizeController getAllotDepartments Error: ", e);
            return ResponseUtil.failedResponse("查询部门人员分派失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/addCorrelate", method = RequestMethod.POST)
    public Object addCorrelate(CorrelatePo correlatePo) {
        try {
            correlateService.addCorrelate(correlatePo);
            return ResponseUtil.successResponse("成功");
        } catch (Exception e) {
            log.error("AuthorizeController getAllotDepartments Error: ", e);
            return ResponseUtil.failedResponse("查询部门人员分派失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/getCorrelateTeanatAndAppByUser", method = RequestMethod.GET)
    public Object getCorrelateTeanatAndAppByUser(String tenantId, String appId, String userId) {
        try {
            UserRelateVo userRelateVo = correlateService.getCorrelateTeanatAndAppByUser(tenantId, appId, userId);
            return ResponseUtil.successResponse(userRelateVo);
        } catch (Exception e) {
            log.error("AuthorizeController getCorrelateByUser Error: ", e);
            return ResponseUtil.failedResponse("查询部门人员分派失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/getCorrelateByUser", method = RequestMethod.GET)
    public Object getCorrelateByUser(String tenantId, String appId, String userId) {
        try {
            UserRelateVo userRelateVo = correlateService.getCorrelateByUser(tenantId, appId, userId);
            return ResponseUtil.successResponse(userRelateVo);
        } catch (Exception e) {
            log.error("AuthorizeController getCorrelateByUser Error: ", e);
            return ResponseUtil.failedResponse("查询部门人员分派失败！", e.getMessage());
        }
    }
}
